﻿namespace SCRS.Bll;

public static class ScrsContext
{
    private static Dictionary<string, object> _dic = new();
    /// <summary>
    /// 属性变化事件,第一个参数是key,第二个参数是新值,第三个参数是旧值
    /// </summary>
    public static event Action<string,object?,object?> OnPropertyChange;

    /// <summary>
    /// 设置上下文属性
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public static void SetProperty(string key, object value)
    {
        _dic.TryGetValue(key, out var oldValue);
        _dic[key] = value;
        OnPropertyChange?.Invoke(key, value, oldValue);
    }

    /// <summary>
    /// 获取上下文属性
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public static T? GetProperty<T>(string key)
    {
        _dic.TryGetValue(key, out var value);
        if (value == null)
        {
            return default;
        }

        return (T)value;
    }
}